package com.pxml2pdf.pdf.Node;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;

public class ButtonNode extends BaseNode {
	
	private static final float BUTTON_FONT_Y_OFF= -2f;
	private static final float BUTTON_EXPANSION_Y= -3f;

	@Override
	public void drawingBox(PdfContentByte cb ) {
		expansion_Y(BUTTON_EXPANSION_Y);
		String btnImage = getBackgroundImage();
		if (btnImage != null && !"".equals(btnImage)) {
			btnImage = btnImage.substring(btnImage.indexOf("/background/")).replace(")", "");
			URL filename = getIcons(btnImage);
			try {
				Image image = Image.getInstance(filename);
				image.setAbsolutePosition(getLlx(), getLly());
				image.setAlignment(Image.UNDERLYING);
				image.scaleAbsolute(getUrx() - getLlx(), getUry() - getLly());
				cb.addImage(image);
			} catch (BadElementException e) {
				e.printStackTrace();
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (DocumentException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void fillValue(PdfContentByte cb,   Font font) throws DocumentException {
		setAlign(PdfContentByte.ALIGN_CENTER);
		setUry(getUry()+BUTTON_FONT_Y_OFF);
		super.fillValue(cb,  font);
	}
}
